package cc.eguid.commandManager;

import cc.eguid.commandManager.commandbuidler.CommandBuidler;
import cc.eguid.commandManager.commandbuidler.CommandBuidlerFactory;
import cc.eguid.commandManager.util.CommonUtil;

public class RstpToHlsUtil {
    private static CommandManager manager = null;
    public static CommandManager getCommandManager(){
        if(null == manager){
            synchronized (RstpToHlsUtil.class){
                if(null == manager){
                    manager = new CommandManagerImpl();
                }
            }
        }
        return manager;
    }

    public static String getHlsUrl(String id , String cameraid){
        try {
            String hlsPath = CommandManager.config.getHlsPath() + id+".m3u8";
            String hlsPlayUrl = CommandManager.config.getHlsPlayUrl() + id+".m3u8";
            CommandBuidler commandBuidler = CommandBuidlerFactory.createBuidler()
                    .add("ffmpeg")
                    .add("-f","rtsp")
                    .add("-rtsp_transport", "tcp")
                    .add("-i", CommandManager.config.getRtspPath() + cameraid)
                    .add("-strict").add("-2")
                    .add("-c:v","libx264")
                    .add("-vsync","2")
                    .add("-r","29.97")
                    .add("-s","480x270")
                    .add("-c:a","aac")
                    .add("-f","hls")
                    .add("-hls_time","4")
                    .add("-hls_list_size","5")
                    .add("-hls_wrap","10")
                    .add("-y").add(hlsPath);
            if(null != getCommandManager().query(id)){
                if(getCommandManager().query(id).getCommand().equals(commandBuidler.get())) {
                    return hlsPlayUrl;
                }else{
                    getCommandManager().stop(id);
                }
            }
            String res = getCommandManager().start(id, commandBuidler);
            if(CommonUtil.isNull(res)){
                return null;
            }
            return hlsPlayUrl;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static boolean stop(String id){
        try{
            if(null == getCommandManager().query(id)){
                return true;
            }
            return getCommandManager().stop(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public static boolean stopAll(){
        try{
            getCommandManager().stopAll();
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public static boolean destory(){
        try{
            getCommandManager().destory();
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String args[]) throws Exception{
        //getRtmpPath("1","1000025@004$1");
        //stop("1");
        //getRtmpPath("1","1000224$2");
        //getRtmpPath("1","1000224$3");
       // getRtmpPath("1","1000224$4");
        //stopAll();
        getHlsUrl("1aaa","1000224$3");
        //getHlsUrl("2bbb","1000088$0");
        //getHlsUrl("3ccc","1000224$0");
        //getHlsUrl("4ddd","1000079$0");
        //getHlsUrl("5eee","1000025@004$13");
        //stopAll();

    }
}
